package cn.sh.yhk.acl.eceptionhandler;


import cn.sh.yhk.base.vo.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import javax.xml.transform.Result;
import java.nio.file.AccessDeniedException;

/**
 * 全局异常处理
 * spring+security项目中的异常
 * 1、security中的两大异常授权异常(AccessDeniedException)+认证异常(AuthenticationException)交由security自己处理
 * 2、其他异常自己处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    //security的授权异常(AccessDeniedException及子类)抛出交由security AuthenticationEntryPoint 处理
    @ExceptionHandler(AccessDeniedException.class)
    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
        throw e;
    }

    //security的认证异常(AuthenticationException及子类)抛出由security AccessDeniedHandler 处理
    @ExceptionHandler(AuthenticationException.class)
    public void authenticationException(AuthenticationException e) throws AuthenticationException {
        throw e;
    }

    //未知异常
    @ExceptionHandler(Exception.class)
    public BaseResponse otherException(Exception e) {
        e.printStackTrace();
        log.error("系统异常 全局拦截异常信息:{}", e.getMessage());
        return BaseResponse.err(e.getMessage());
    }
}